package zmee.bid.formplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.util.EventObject;

public class PmctOutaddagreePlugin extends AbstractFormPlugin implements BeforeF7SelectListener {

    @Override
    public void afterBindData(EventObject e) {
       Object sqr=this.getModel().getValue("sqr");
        if(sqr==null){
            DynamicObject user = BusinessDataServiceHelper.loadSingle(UserServiceHelper.getCurrentUserId(), "bos_user");
            this.getModel().setValue("sqr",user);
            DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
            for (DynamicObject ent:entryentity) {
                if(!ent.getBoolean("ispartjob")){
                    this.getModel().setValue("htcbbm",ent.getString("dpt.id"));
                }
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        ChangeData data = e.getChangeSet()[0];
        if(name.equals("sqr")){
            Object newValue = data.getNewValue();
            if(newValue != null){
                DynamicObject row = (DynamicObject)newValue;
                DynamicObject user = BusinessDataServiceHelper.loadSingle(row.getPkValue(), row.getDynamicObjectType().getName());
                DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
                for (DynamicObject ent:entryentity) {
                    if(!ent.getBoolean("ispartjob")){
                        this.getModel().setValue("htcbbm",ent.getString("dpt.id"));
                        break;
                    }
                }
            }else {
                this.getModel().setValue("htcbbm",newValue);
            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {

    }
}
